<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use function Symfony\Component\String\s;

class DistributionsStatistics extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:DistributionsStatistics {start_date} {end_date}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '省代数据统计';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $start_date = $this->argument('start_date');
        $end_date = $this->argument('end_date');
        $this->statOldData($start_date, $end_date);
        return 0;
    }

    public function statOldData($start_month, $end_month)
    {
        $start_month = strtotime($start_month);
        $end_month = strtotime($end_month);
        for ($time = $start_month; $time < $end_month; $time += 86400) {
            $start_date = date('Y-m-d', $time);
            $end_date = date('Y-m-d', $time + 86400);
            echo $start_date; echo PHP_EOL;
            $sales = DB::table('roll_sales')->leftJoin('users', 'users.id', 'roll_sales.sold_to_user_id')
                ->leftJoin('roll_master', 'roll_master.id', 'roll_sales.roll_id')
                ->where('roll_sales.created_at', '>=', $start_date)
                ->where('roll_sales.created_at', '<', $end_date)
                ->select('roll_sales.*', 'users.user_type', 'roll_master.product_type_id', 'roll_master.film_type_id', 'roll_master.length as master_length')
                ->get();
            if (empty($sales) || $sales->isEmpty()) {
                continue;
            }

            foreach ($sales as $sale) {
                if ($sale->master_length <= 0) {
                    continue;
                }
                $user_sales = DB::table('xpel_user_sales')->where('user_id', $sale->sold_to_user_id)
                    ->where('create_date', date('Y-m-d', strtotime($sale->date_sold)))
                    ->first();

                if (in_array($sale->film_type_id, [27, 74])) {
                    $len = round($sale->length / 1500, 2);
                } else {
                    $len = round($sale->length / $sale->master_length, 2);
                }

                var_dump($user_sales);
                if (empty($user_sales)) {
                    $insertData = [
                        'user_id'   => $sale->sold_to_user_id,
                        'user_type' => $sale->user_type ?? 0,
                        'sales_num' => $len,
                        'create_date' => date('Y-m-d', strtotime($sale->created_at)),
                    ];
                    DB::table('xpel_user_sales')->insert($insertData);
                    continue;
                }

                $updateData = [
                    'sales_num' => $len + $user_sales->sales_num,
                ];

                DB::table('xpel_user_sales')->where('id', $user_sales->id)->update($updateData);
            }
        }
    }
}
